﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using ExFlash.Records;

namespace ExFlash.Tags
{
    public class DefineEditTextTag : Tag, IReferenceCharacterId
    {
        private readonly int _swfVersion;

        public ushort CharacterId
        {
            get;
            protected set;
        }

        public RectangleRecord Bounds
        {
            get;
            protected set;
        }

        public Boolean HasText
        {
            get;
            protected set;
        }

        public Boolean WordWrap
        {
            get;
            protected set;
        }

        public Boolean Multiline
        {
            get;
            protected set;
        }

        public Boolean Password
        {
            get;
            protected set;
        }

        public Boolean ReadOnly
        {
            get;
            protected set;
        }

        public Boolean HasTextColor
        {
            get;
            protected set;
        }

        public Boolean HasMaxLength
        {
            get;
            protected set;
        }

        public Boolean HasFont
        {
            get;
            protected set;
        }

        public Boolean HasFontClass
        {
            get;
            protected set;
        }

        public Boolean AutoSize
        {
            get;
            protected set;
        }

        public Boolean HasLayout
        {
            get;
            protected set;
        }

        public Boolean NoSelect
        {
            get;
            protected set;
        }

        public Boolean Border
        {
            get;
            protected set;
        }

        public Boolean WasStatic
        {
            get;
            protected set;
        }

        public Boolean Html
        {
            get;
            protected set;
        }

        public Boolean UseOutlines
        {
            get;
            protected set;
        }

        public ushort FontId
        {
            get;
            protected set;
        }

        public StringRecord FontClass 
        { 
            get; 
            protected set; 
        }

        public ushort FontHeight
        {
            get;
            protected set;
        }

        public RGBARecord TextColor
        {
            get;
            protected set;
        }

        public ushort MaxLength
        {
            get;
            protected set;
        }

        public Byte Align
        {
            get;
            protected set;
        }
        
        public ushort LeftMargin
        {
            get;
            protected set;
        }

        public ushort RightMargin
        {
            get;
            protected set;
        }

        public ushort Indent
        {
            get;
            protected set;
        }

        public short Leading
        {
            get;
            protected set;
        }

        public StringRecord VariableName
        {
            get;
            protected set;
        }

        public StringRecord InitialText
        {
            get;
            protected set;
        }

        public DefineEditTextTag(int len, int version, int offset)
            : base(TagCodes.DefineEditText, len, offset)
        {
            _swfVersion = version;
        }

        public override void SwfDeserialize(BinaryBitReader reader)
        {
            CharacterId = reader.ReadUInt16();

            Bounds = new RectangleRecord();
            Bounds.SwfDeserialize(reader);

            HasText = reader.ReadBoolean();
            WordWrap = reader.ReadBoolean();
            Multiline = reader.ReadBoolean();
            Password = reader.ReadBoolean();
            ReadOnly = reader.ReadBoolean();
            HasTextColor = reader.ReadBoolean();
            HasMaxLength = reader.ReadBoolean();
            HasFont = reader.ReadBoolean();
            HasFontClass = reader.ReadBoolean();
            AutoSize = reader.ReadBoolean();
            HasLayout = reader.ReadBoolean();
            NoSelect = reader.ReadBoolean();
            Border = reader.ReadBoolean();
            WasStatic = reader.ReadBoolean();
            Html = reader.ReadBoolean();
            UseOutlines = reader.ReadBoolean();

            reader.AlignBits();

            if (HasFont)
            {
                FontId = reader.ReadUInt16();
            }

            if (HasFontClass)
            {
                FontClass = new StringRecord(_swfVersion);
                FontClass.SwfDeserialize(reader);
            }

            if (HasFont)
            {
                FontHeight = reader.ReadUInt16();
            }

            if (HasTextColor)
            {
                TextColor = new RGBARecord();
                TextColor.SwfDeserialize(reader);
            }

            if (HasMaxLength)
            {
                MaxLength = reader.ReadUInt16();
            }

            if (HasLayout)
            {
                Align = reader.ReadByte();
                LeftMargin = reader.ReadUInt16();
                RightMargin = reader.ReadUInt16();
                Indent = reader.ReadUInt16();
                Leading = reader.ReadInt16();
            }

            VariableName = new StringRecord(_swfVersion);
            VariableName.SwfDeserialize(reader);

            if (HasText)
            {
                InitialText = new StringRecord(_swfVersion);
                InitialText.SwfDeserialize(reader);
            }
        }

        public override void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }

        public bool IsReferencing
        {
            get { throw new NotImplementedException(); }
        }

        public ushort ReferencedId
        {
            get { return FontId; }
        }

        public IEnumerable<Type> ReferencedAttributes
        {
            get { throw new NotImplementedException(); }
        }

        public Type ReferencedType
        {
            get { throw new NotImplementedException(); }
        }
    }
}
